package edu.xidian.bdilab.jrbigdata.service.bank.demandcheck.impl;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonResponse;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import edu.xidian.bdilab.jrbigdata.controller.bank.BankCode;
import edu.xidian.bdilab.jrbigdata.controller.government.GovernmentCode;
import edu.xidian.bdilab.jrbigdata.controller.response.ResponseResult;
import edu.xidian.bdilab.jrbigdata.domain.EnterpriseInfo;
import edu.xidian.bdilab.jrbigdata.domain.EnterpriseReq;
import edu.xidian.bdilab.jrbigdata.mapper.EnterpriseInfoMapper;
import edu.xidian.bdilab.jrbigdata.mapper.EnterpriseReqMapper;
import edu.xidian.bdilab.jrbigdata.service.bank.demandcheck.DemandCheckService;
import edu.xidian.bdilab.jrbigdata.util.AliyunConfigUtil;
import edu.xidian.bdilab.jrbigdata.util.GuavaCacheUtil;
import edu.xidian.bdilab.jrbigdata.util.MessageUtil;
import org.apache.poi.ss.formula.functions.T;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static java.lang.Boolean.FALSE;
import static java.lang.Boolean.TRUE;

@Service
public class DemandCheckServiceImpl implements DemandCheckService {

    private static final Logger logger = LoggerFactory.getLogger(DemandCheckServiceImpl.class);


    @Autowired
    EnterpriseReqMapper enterpriseMapper;

    @Autowired
    private EnterpriseInfoMapper enterpriseInfoMapper;

    @Override
    public ResponseResult demandcheck(Integer loanLimit, Double lowerLimit, Double upperLimit, Integer loanPeriod, Integer guaranteeMode)
    {
        QueryWrapper<EnterpriseReq> enterprise_reqWrapper = new QueryWrapper<>();

        EnterpriseReq enterprise_req;

        ResponseResult responseResult=new ResponseResult();
        enterprise_reqWrapper.eq(loanLimit!=null,"loan_limit",loanLimit);
        enterprise_reqWrapper.eq(loanPeriod!=null,"loan_period",loanPeriod);
        enterprise_reqWrapper.eq(guaranteeMode!=null,"guarantee_mode",guaranteeMode);
        enterprise_reqWrapper.ge(lowerLimit!=null,"rate_lower_limit",lowerLimit);
        enterprise_reqWrapper.le(upperLimit!=null,"rate_upper_limit",upperLimit);
        List<EnterpriseReq>list=enterpriseMapper.selectList(enterprise_reqWrapper);
        responseResult.setCode(BankCode.CHENCK_FAILED.getCode());
        responseResult.setMessage(BankCode.CHENCK_FAILED.getMessage());
        responseResult.setSuccess(FALSE);
        if(list.size()==0)
        {
            responseResult.setCode(BankCode.CHECK_NONE.getCode());
            responseResult.setMessage(BankCode.CHECK_NONE.getMessage());
            responseResult.setSuccess(FALSE);
        }


        else
        {
            responseResult.setData(list);
            responseResult.setCode(BankCode.CHECK_SUCCESS.getCode());
            responseResult.setMessage(BankCode.CHECK_SUCCESS.getMessage());
            responseResult.setSuccess(TRUE);

        }

        return  responseResult;
    }

    @Override
    public ResponseResult demandCheckSendMessage(String socialCode) {
        logger.info("发送短信的代码={}",socialCode);
        ResponseResult responseResult=new ResponseResult();
        QueryWrapper<EnterpriseInfo> queryWrapper = new QueryWrapper<EnterpriseInfo>();
        queryWrapper.lambda().eq(EnterpriseInfo::getSocialCode,socialCode);
        EnterpriseInfo enterpriseInfo = enterpriseInfoMapper.selectOne(queryWrapper);
        if(null == enterpriseInfo){
            responseResult.setCode(BankCode.CHECK_NONE.getCode());
            responseResult.setMessage("没有获取到企业信息");
            responseResult.setSuccess(FALSE);
           return responseResult;
        }
        String contactPhone = enterpriseInfo.getContactPhone();
        if(StringUtils.isEmpty(contactPhone)){
            responseResult.setCode(BankCode.CHECK_NONE.getCode());
            responseResult.setMessage("没有查询到企业联系人手机号");
            responseResult.setSuccess(FALSE);
            return responseResult;
        }

        String messg = (String) GuavaCacheUtil.get(contactPhone+"CheckSendMessage");
        if(!StringUtils.isEmpty(messg)){
            responseResult.setSuccess(false);
            responseResult.setCode(GovernmentCode.SEND_CODE_REPEAT.getCode());
            responseResult.setMessage(GovernmentCode.SEND_CODE_REPEAT.getMessage());
            return responseResult;
        }

        /**
         * 发送短信验证码
         */
        Map<String,String> param = new HashMap<>();
        param.put("name",enterpriseInfo.getEnterpriseName());
        CommonResponse commonResponse = MessageUtil.aliyunsendSms(AliyunConfigUtil.APP_ID,AliyunConfigUtil.APP_SECRET,contactPhone,
                AliyunConfigUtil.SIGNNAME,AliyunConfigUtil.TEMPLATE_CODE_SEND_BANK_NOtfy, param);
        JSONObject json = JSONObject.parseObject(commonResponse.getData());
        String resultCode =(String)json.get("Code");
        String message =(String)json.get("Message");
        if("OK".equals(resultCode) && "OK".equals(message)){
            GuavaCacheUtil.put(contactPhone+"CheckSendMessage","CheckSendMessage");
            responseResult.setSuccess(true);
            responseResult.setCode(GovernmentCode.SEND_CODE_SUCCESS.getCode());
            responseResult.setMessage("短信通知已发送");
            return responseResult;
        }
        logger.info("end发送短信的代码={}",JSONObject.toJSON(commonResponse));
        return responseResult;
    }
}
